<?php
if (!defined('FosfoaSystem')) { exit(1);}
import ($GLOBALS['spConfig']['controller_path'].'/general.php');
class data extends general
{
    var $attachDir='bfdata';
    public function __construct()
    {
        parent::__construct();
    }
    function index()
    {
        $basedir_file=$this->attachDir;  
        if ($handle = opendir($basedir_file))
        {
              $dbarr=array();
              while (1)
              {
                    $ufile = readdir($handle);
                    if ($ufile == "") break;
                    if (($ufile!=".")&($ufile!= "..")&($ufile!= "index.htm"))
                    {
                          $tfile=iconv('GB2312', 'UTF-8', $ufile);
                          $dbarr[]=$tfile;
                    }
              }
        }
        $this->dbarr=$dbarr;
        $this->tt = "数据管理首页";
        $this->display("admin/data_index.html");
    }
    //下载文件
    function down()
    {
        $file_dir=$this->attachDir."/";
        $file_name=$this->spArgs('db');
        $msg="下载数据库".$file_name;
        Savelog($msg);
        if (!file_exists($file_dir.$file_name)) {
            echo "Can't find file.";
            exit;
        } else {
            $file = fopen($file_dir . $file_name,"r");
            Header("Content-type: application/octet-stream");
            Header("Accept-Ranges: bytes");
            Header("Accept-Length: ".filesize($file_dir . $file_name));
            Header("Content-Disposition: attachment; filename=" . $file_name);
            echo fread($file,filesize($file_dir . $file_name));
            fclose($file); 
            exit;
        }
    }
    //删除文件
    function deldb()
    {
        $file_dir=$this->attachDir."/";
        $file=$file_dir.$this->spArgs('db');
        if (!unlink($file))
          {
            $msg='删除文件失败-'.$file;
          //$this->error('删除文件失败-'.$file);
          }
        else
          {
            $msg=$file.'-文件删除成功';
          
          }
        Savelog($msg);  
        $this->success($msg); 
    }
    
    //备份数据
    function nowbf()
    {
        $action=$this->spArgs('action');
            switch ($action)
            {
            case 'Sqlite':
              $bftype = $this->sqlite_bf();
              break;
            case 'Mysql':
              $paytype = T("payys");
              break;
            default:
              $paytype = T("CheckoutSuccesSText");
            }	
		    echo json_encode( $bftype ); // 返回（显示）JSON结果
    }
    
    /*
    * 备份SQLITE数据文件
    */
    function sqlite_bf()
    {
        $attachDir=$this->attachDir;
        if(!is_dir($attachDir))
		{
			@mkdir($attachDir, 0705);
        	@chmod($attachDir, 0705);
			@fclose(fopen($attachDir.'/index.htm', 'w'));
		}
       $filename=$attachDir.'/'.date('Y-m-d-H-i-s').'.db';
       if(copy("fosf.db",$filename))
       {
        $status=1;
        $msg='备份数据文件成功<br/>'.$filename;
       }else{
        $status=1;
        $msg='备份数据文件失败';
       }
       Savelog($msg);
       return array('status'=>$status,'message'=>$msg);
    }
    /*
    * 还原SQLITE数据文件
    */
    function sqlite_hy()
    {
        $file_dir=$this->attachDir."/";
        $file=$file_dir.$this->spArgs('db');
       if(copy($file,'fosf.db'))
       {
        $msg=$file.'-文件还原成功';
       }else{
        $msg='文件还原失败-'.$file;
       }
       Savelog($msg);
       $this->success($msg);
    }    

    //压缩数据
    function nowys()
    {
        $action=$this->spArgs('action');
        if($action=='Sqlite'){
            $bftype = $this->sqlite_bf();
            if($bftype[status]==1){
                $bfmsg=$bftype[msg];
            }
        $sqlstr='VACUUM';
        spClass('newsModel')->runSql($sqlstr);
			$result = array(
				'status' => 1,
				'message' => '压缩完成,如果出错，请还原！'.$bfmsg,
			);
        Savelog($result[message]);
        echo json_encode( $result );
       }   
    } 
}